﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MediaManager
{
    public partial class UpdatePassword : Form
    {
        private UpdatePassword()
        {
            InitializeComponent();
        }
        private string _name = "";
        public UpdatePassword(string name)
        {
            InitializeComponent();
            _name = name;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpdatePassword_Load(object sender, EventArgs e)
        {
            txtOldPassword.Text = "";
            txtPassword1.Text = "";
            txtPassword2.Text = "";
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtPassword1.Text == txtPassword2.Text)
                {
                    Login login = _dataContext.Login.Where(p => p.name == _name).Single();
                    string pass = Crypt.Cryption.Instance.EncryptDES(txtOldPassword.Text);
                    if (login.password == pass)
                    {
                        login.password = Crypt.Cryption.Instance.EncryptDES(txtPassword1.Text);
                        _dataContext.SubmitChanges();
                        GlobalBase.Global.Instance.ShowMessage("更改密码成功!");
                        btnCancel_Click(sender, e);
                    }
                    else
                    {
                        GlobalBase.Global.Instance.ShowMessage("输入的老密码不正确！");
                    }
                }
                else
                {
                    GlobalBase.Global.Instance.ShowMessage("输入密码不匹配！");
                }
            }
            catch (Exception ex)
            {
                GlobalBase.CatchException.DealWithException(ex);
                GlobalBase.Global.Instance.ShowMessage("更改密码失败！");
            }
        }

        private void txtPassword2_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnOk_Click(sender, e);
            }
        }
        DataClasses2DataContext _dataContext = new DataClasses2DataContext();

    }
}
